//
//  YJQSportingViewController.m
//  GoodLuck
//
//  Created by 杨瑞 on 16/12/12.
//  Copyright © 2016年 杨瑞. All rights reserved.
//

#import "YJQSportingViewController.h"
#import "YJQMapViewController.h"
#import "UIColor+YJQAddition.h"
#import "YJQSpeaker.h"
#import "YJQSportCameraViewController.h"

@interface YJQSportingViewController ()<YJQMapViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *distanceLable;
@property (weak, nonatomic) IBOutlet UILabel *timeLable;
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLable;
@property (weak, nonatomic) IBOutlet UIButton *mapButton;
@property (nonatomic,strong) YJQMapViewController *mapVC;
@property (weak, nonatomic) IBOutlet UIButton *pauseBtn;
@property (weak, nonatomic) IBOutlet UIButton *continueBtn;
@property (weak, nonatomic) IBOutlet UIButton *finishedBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pauseButtonCenterXCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueButtonCenterXCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishedButtonCenterXCons;
@end

@implementation YJQSportingViewController {
    YJQSpeaker *_sportSpeaker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
//    [self setupUI];
    _sportSpeaker = [YJQSpeaker new];
    [self setupMapViewController];
    [self setupBackgroundLayer];
}

/**
 设置背景渐变颜色
 */
- (void)setupBackgroundLayer {
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.bounds = self.view.bounds;
    layer.position = self.view.center;
    CGColorRef color1 = [UIColor yjq_colorWithHex:0x0e1428].CGColor;
    CGColorRef color2 = [UIColor yjq_colorWithHex:0x406479].CGColor;
    CGColorRef color3 = [UIColor yjq_colorWithHex:0x406578].CGColor;
    layer.colors = @[(__bridge UIColor *)color1,(__bridge UIColor *)color2,(__bridge UIColor *)color3];
    layer.locations = @[@0,@0.6,@1];
    [self.view.layer insertSublayer:layer atIndex:0];
}

// 所有子控件布局完成，适合进一步设置其他控件的位置
-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 设置罗盘位置
    CGFloat x = _mapButton.center.x - _mapVC.mapView.compassSize.width * 0.5;
    CGFloat y = _mapButton.center.y - _mapVC.mapView.compassSize.height * 0.5;
    _mapVC.mapView.compassOrigin = CGPointMake(x, y);
}

- (IBAction)showMapView {
    [self presentViewController:_mapVC animated:YES completion:nil
     ];
}
#pragma mark - 监听方法
/**
 修改运动状态

 @param sender button
 */
-(IBAction)changeSportState:(UIButton *)sender {
    YJQSportState state = sender.tag;
    //修改地图控制器的运动状态
    _mapVC.sportTraking.state = state;
    
    CGFloat offset = (state == YJQSportStatePause) ? -80 : 80;
    _pauseButtonCenterXCons.constant += offset;
    _continueButtonCenterXCons.constant += offset;
    _finishedButtonCenterXCons.constant -= offset;
    
    [UIView animateWithDuration:0.25 animations:^{
        _pauseBtn.alpha = (sender != _pauseBtn);
        
        [self.view layoutIfNeeded];
    }];
    
    [_sportSpeaker sportStateChangeWithState:state];
}
//#pragma mark -设置界面
//
////添加地图父子控制器
//- (void)setupUI {
////    //实例化子控制器
//    //获取子控制器
//    for (UIViewController *child in self.childViewControllers) {
//        if ([child isKindOfClass:[YJQMapViewController class]]) {
//            _mapVC = (YJQMapViewController *)child;
//            break;
//        }
//    }
////    YJQMapViewController *mapVC = [YJQMapViewController new];
////    //添加自控制器
////    [self addChildViewController:mapVC];
////    //添加自控制器的视图
////    [self.view addSubview:mapVC.view];
////    mapVC.view.frame = self.view.bounds;
//    //数据传递
//    _mapVC.sportTraking = [[YJQSportTypeModel alloc]initWithSportType:_sportType state:YJQSportStateContinue];
////    //告诉控制器完成添加自控制器
////    [mapVC didMoveToParentViewController:self];
//    
//}
- (IBAction)showCameraViewController {
    YJQSportCameraViewController *vc = [YJQSportCameraViewController new];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)setupMapViewController {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YJQSporting" bundle:nil];
    _mapVC = [sb instantiateViewControllerWithIdentifier:@"mapViewController"];
    _mapVC.sportTraking = [[YJQSportTypeModel alloc] initWithSportType:_sportType state:YJQSportStateContinue];
    NSLog(@"%@",_mapVC);
    _mapVC.delegate = self;
    [_sportSpeaker startSportWithType:_sportType];
}

#pragma mark - YJQMapViewControllerDelegate
-(void)mapViewControllerDidChangedData:(YJQMapViewController *)vc {
    YJQSportTypeModel *sportTraking = vc.sportTraking;
    _distanceLable.text = [NSString stringWithFormat:@"%0.2f",sportTraking.totleDistance];
    _timeLable.text = sportTraking.totalTimes;
    _avgSpeedLable.text = [NSString stringWithFormat:@"%0.2f",vc.sportTraking.avgSpeed];
    if (sportTraking.state == YJQSportStatePause && _pauseBtn.alpha == 1) {
        [self changeSportState:_pauseBtn];
    }
    if (sportTraking.state == YJQSportStateContinue && _pauseBtn.alpha == 0) {
        [self changeSportState:_continueBtn];
    }
    [_sportSpeaker reportWithDistance:sportTraking.totleDistance time:sportTraking.totleTime speed:sportTraking.avgSpeed];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
